package com.cn.ssm.controller;

import com.alibaba.fastjson.JSON;
import com.cn.ssm.model.*;
import com.cn.ssm.permisson.PermissionManager;
import com.cn.ssm.service.*;

import org.apache.commons.codec.digest.DigestUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;


import javax.annotation.Resource;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.xml.registry.infomodel.User;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/user")
public class UserController {
   Logger logger=Logger.getLogger(UserController.class);

   @Resource
   BureauInfoService bureauInfoService;

	@Resource
	UserInfoService userInfoService;

	@Resource
	UserRoleInfoService  userRoleInfoService;

	@Resource
	RoleInfoService roleInfoService;

	@Resource
	CityInfoService cityInfoService;

   @RequestMapping("/init")
   public ModelAndView initBureauInfo(){
	   System.out.println("initBureauInfo-----start");
	   ModelAndView mv=new ModelAndView("/user/login");
	   List<BureauInfo> res= bureauInfoService.selectBureauInfo("16101020000");
	   logger.debug("res"+res);
	   mv.addObject("initBureauInfo", res);
	   mv.addObject("test", "你好，spring！我来了");
	   System.out.println("initBureauInfo----end");
	   return mv;
   }


	@RequestMapping(value="/validate")
	public  void userVlidate(@RequestBody  UserInfo userinfo,HttpServletResponse response) throws IOException{
		System.out.println("userLoginVlidate-----start");
		logger.debug("username:"+userinfo.getUsername());
		logger.debug("password:"+userinfo.getUserpassword());
		ModelAndView mv=new ModelAndView("/user/mains");
		UserInfo user=userInfoService.selectByKeys(userinfo);
		String jsontostring="";
        if(user==null){
			jsontostring=JSON.toJSONString("0");
		}else{
			jsontostring= JSON.toJSONString(user);
		}
		response.setCharacterEncoding("utf-8");
		response.getWriter().write(jsontostring);
		response.getWriter().close();
	}

	@RequestMapping("/login")
	public ModelAndView userLogin(@RequestParam String sequencenum, HttpServletRequest request){
		System.out.println("userLogin-----start");
		logger.debug("sequencenum:"+sequencenum);

		ModelAndView mv=new ModelAndView("/user/mains");
        //查询用户基本信息，关联机构和部门等相关信息
		Map param=new HashMap();
		param.put("sequencenum",sequencenum);
		List<Map<String,Object>> list=userInfoService.selectBySeques(sequencenum);
		mv.addObject("list", list);


		System.out.println("userLogin----end");
		return mv;
	}

	//检查用户信息 用户不存在  0  密码错误：2   校验通过 1
	public String checkUser(String username,String pwd){
		UserInfo userinfo=userInfoService.selectByname(username);
		if(userinfo==null){
			return "0";
		}else if(userinfo.getUserpassword()!=null&&!DigestUtils.md5Hex(pwd).equals(userinfo.getUserpassword())){
			return "2";
		}else{
			return "1";
		}
	}


    //保存日志
	@RequestMapping(value="/savelog")
	public  void saveLog(@RequestBody  UserInfo userinfo,HttpServletResponse response) throws IOException{
		System.out.println("userLoginVlidate-----start");
		logger.debug("username:"+userinfo.getUsername());
		logger.debug("password:"+userinfo.getUserpassword());
		UserInfo user=userInfoService.selectByKeys(userinfo);
		String jsontostring="";
		if(user==null){
			jsontostring=JSON.toJSONString("0");
		}else{
			jsontostring= JSON.toJSONString(user);
		}
		response.setCharacterEncoding("utf-8");
		response.getWriter().write(jsontostring);
		response.getWriter().close();
	}




}
